package com.bw.lyc.mvp.view.mine;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bw.lyc.R;
import com.bw.lyc.mvp.base.model.BaseEntity;
import com.bw.lyc.mvp.base.view.BaseFragment;
import com.bw.lyc.mvp.contract.UserContract;
import com.bw.lyc.mvp.di.DaggerUserComponent;
import com.bw.lyc.mvp.di.UserModules;
import com.bw.lyc.mvp.model.db.DBManager;
import com.bw.lyc.mvp.model.db.UserEntityDao;
import com.bw.lyc.mvp.model.entity.UserEntity;
import com.bw.lyc.mvp.model.entity.UserItemEntity;
import com.bw.lyc.mvp.model.entity.UserMenuEntity;
import com.bw.lyc.mvp.presenter.UserPresenter;
import com.bw.lyc.mvp.view.mine.adapter.UserAdapter;
import com.bw.lyc.mvp.view.mine.adapter.UserMenuAdapter;
import com.bw.lyc.util.UnLine;

import java.util.List;


public class MineFragment extends BaseFragment<UserPresenter> implements UserContract.IUserView, View.OnClickListener {

    private View menuView, timeLineView;
    private RecyclerView mine_rv;
    private RecyclerView menu_rv;
    private UserAdapter adapter;
    private UserMenuAdapter menuAdapter;
    private LinearLayoutManager linearLayoutManager;
    private ImageView menu_head_img;
    private TextView mine_name;
    private getUserBroadcast getUserBroadcast;


    @Override
    public int bindLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView() {
        mine_rv = f(R.id.mine_rv);
        menuView = LayoutInflater.from(getContext()).inflate(R.layout.view_mine_menu, null);
        timeLineView = LayoutInflater.from(getContext()).inflate(R.layout.view_mine_speed, null);
        menu_rv = menuView.findViewById(R.id.menu_rv);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        menu_rv.setLayoutManager(gridLayoutManager);
        menuAdapter = new UserMenuAdapter();
        menu_rv.setAdapter(menuAdapter);
        linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mine_rv.setLayoutManager(linearLayoutManager);
        adapter = new UserAdapter();
        adapter.addHeaderView(menuView);
        adapter.addHeaderView(timeLineView);
        mine_rv.setAdapter(adapter);
        mine_rv.addItemDecoration(new UnLine(getContext()));
        menu_head_img = menuView.findViewById(R.id.menu_head_img);
        menu_head_img.setOnClickListener(this::onClick);
        mine_name = menuView.findViewById(R.id.mine_name);
        try {
            List<UserEntity> list = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).where(UserEntityDao.Properties.Id.eq("0")).list();
            for (int i = 0; i < list.size(); i++) {
                String user_name = list.get(i).getUser_name();
                mine_name.setText(user_name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            UserEntity unique = DBManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).where(UserEntityDao.Properties.Id.eq(0)).unique();
            String user_img = unique.getUser_img();
            Glide.with(getContext()).load(user_img).circleCrop().into(menu_head_img);

        } catch (Exception e) {
            e.printStackTrace();
        }


        getUserBroadcast = new getUserBroadcast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("path");
        intentFilter.addAction("headImg");
        intentFilter.addAction("username");
        getActivity().registerReceiver(getUserBroadcast, intentFilter);


    }

    class getUserBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("path")) {
                String path = intent.getStringExtra("path");
                Glide.with(getContext()).load(path).circleCrop().into(menu_head_img);
            }
            if (intent.getAction().equals("headImg")) {
                String path1 = intent.getStringExtra("headImg");
                Glide.with(getContext()).load(path1).circleCrop().into(menu_head_img);
            }
            if (intent.getAction().equals("username")) {
                String username = intent.getStringExtra("username");
                mine_name.setText(username);
            }
        }
    }

    @Override
    public void inject() {
        DaggerUserComponent.builder().userModules(
                new UserModules(this)
        ).build().inject(this);
    }

    @Override
    public void initData() {
        p.initData();
    }

    @Override
    public void refresh(BaseEntity entity) {

    }

    @Override
    public void refreshMenu(List<UserMenuEntity> menues) {
        menuAdapter.setNewInstance(menues);
    }

    @Override
    public void refreshItemes(List<UserItemEntity> itemes) {
        adapter.setNewInstance(itemes);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.menu_head_img) {
            try {
                List<UserEntity> list = DBManager.getInstance().getDaoSession().loadAll(UserEntity.class);
                if (list.size() == 0) {
                    startPage(LoginActivity.class);
                } else {
                    startPage(UpdateActivity.class);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(getUserBroadcast);
    }
}